using JGSY.CMS.LowCode.Platform.Application.DTOs;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Services.Search
{
    /// <summary>
    /// 通用搜索服务接口
    /// 提供跨业务实体的统一搜索功能
    /// </summary>
    public interface ISearchService
    {
        /// <summary>
        /// 全局搜索
        /// </summary>
        /// <param name="keyword">搜索关键词</param>
        /// <param name="filters">搜索过滤器</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>搜索结果</returns>
        Task<SearchResultDto> GlobalSearchAsync(string keyword, SearchFiltersDto? filters = null, int pageIndex = 0, int pageSize = 20);

        /// <summary>
        /// 按实体类型搜索
        /// </summary>
        /// <param name="entityType">实体类型</param>
        /// <param name="keyword">搜索关键词</param>
        /// <param name="filters">搜索过滤器</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>搜索结果</returns>
        Task<SearchResultDto> SearchByEntityTypeAsync(string entityType, string keyword, SearchFiltersDto? filters = null, int pageIndex = 0, int pageSize = 20);

        /// <summary>
        /// 搜索建议
        /// </summary>
        /// <param name="keyword">关键词</param>
        /// <param name="maxSuggestions">最大建议数量</param>
        /// <returns>搜索建议列表</returns>
        Task<IEnumerable<string>> GetSearchSuggestionsAsync(string keyword, int maxSuggestions = 10);

        /// <summary>
        /// 重建搜索索引
        /// </summary>
        /// <param name="entityType">实体类型，为空则重建所有索引</param>
        /// <returns>重建结果</returns>
        Task<bool> RebuildIndexAsync(string? entityType = null);

        /// <summary>
        /// 删除实体的搜索索引
        /// </summary>
        /// <param name="entityType">实体类型</param>
        /// <param name="entityId">实体ID</param>
        /// <returns>删除结果</returns>
        Task<bool> DeleteEntityIndexAsync(string entityType, string entityId);

        /// <summary>
        /// 更新实体的搜索索引
        /// </summary>
        /// <param name="entityType">实体类型</param>
        /// <param name="entityId">实体ID</param>
        /// <param name="entityData">实体数据</param>
        /// <returns>更新结果</returns>
        Task<bool> UpdateEntityIndexAsync(string entityType, string entityId, object entityData);
    }

    /// <summary>
    /// 搜索过滤器
    /// </summary>
    public class SearchFiltersDto
    {
        /// <summary>
        /// 实体类型过滤器
        /// </summary>
        public List<string>? EntityTypes { get; set; }

        /// <summary>
        /// 创建时间范围过滤器
        /// </summary>
        public DateRangeFilter? CreatedDateRange { get; set; }

        /// <summary>
        /// 更新时间范围过滤器
        /// </summary>
        public DateRangeFilter? UpdatedDateRange { get; set; }

        /// <summary>
        /// 标签过滤器
        /// </summary>
        public List<string>? Tags { get; set; }

        /// <summary>
        /// 分类过滤器
        /// </summary>
        public List<string>? Categories { get; set; }

        /// <summary>
        /// 状态过滤器
        /// </summary>
        public List<string>? Statuses { get; set; }

        /// <summary>
        /// 作者过滤器
        /// </summary>
        public List<string>? Authors { get; set; }

        /// <summary>
        /// 自定义属性过滤器
        /// </summary>
        public Dictionary<string, object>? CustomFilters { get; set; }
    }

    /// <summary>
    /// 日期范围过滤器
    /// </summary>
    public class DateRangeFilter
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartDate { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndDate { get; set; }
    }

    /// <summary>
    /// 搜索结果
    /// </summary>
    public class SearchResultDto
    {
        /// <summary>
        /// 搜索关键词
        /// </summary>
        public string Keyword { get; set; } = string.Empty;

        /// <summary>
        /// 总结果数
        /// </summary>
        public long TotalCount { get; set; }

        /// <summary>
        /// 搜索耗时（毫秒）
        /// </summary>
        public long ElapsedMilliseconds { get; set; }

        /// <summary>
        /// 结果列表
        /// </summary>
        public List<SearchItemDto> Items { get; set; } = new();

        /// <summary>
        /// 聚合结果
        /// </summary>
        public SearchAggregationsDto? Aggregations { get; set; }

        /// <summary>
        /// 搜索建议
        /// </summary>
        public List<string>? Suggestions { get; set; }
    }

    /// <summary>
    /// 搜索项
    /// </summary>
    public class SearchItemDto
    {
        /// <summary>
        /// 实体ID
        /// </summary>
        public string Id { get; set; } = string.Empty;

        /// <summary>
        /// 实体类型
        /// </summary>
        public string EntityType { get; set; } = string.Empty;

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; } = string.Empty;

        /// <summary>
        /// 描述/摘要
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 内容（可能包含高亮标记）
        /// </summary>
        public string Content { get; set; } = string.Empty;

        /// <summary>
        /// 链接地址
        /// </summary>
        public string Url { get; set; } = string.Empty;

        /// <summary>
        /// 缩略图
        /// </summary>
        public string? Thumbnail { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        public List<string>? Tags { get; set; }

        /// <summary>
        /// 分类
        /// </summary>
        public string? Category { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public string? Status { get; set; }

        /// <summary>
        /// 作者
        /// </summary>
        public string? Author { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdatedAt { get; set; }

        /// <summary>
        /// 评分
        /// </summary>
        public float Score { get; set; }

        /// <summary>
        /// 高亮片段
        /// </summary>
        public List<string>? Highlights { get; set; }

        /// <summary>
        /// 自定义属性
        /// </summary>
        public Dictionary<string, object>? CustomProperties { get; set; }
    }

    /// <summary>
    /// 搜索聚合结果
    /// </summary>
    public class SearchAggregationsDto
    {
        /// <summary>
        /// 按实体类型聚合
        /// </summary>
        public Dictionary<string, long>? EntityTypes { get; set; }

        /// <summary>
        /// 按分类聚合
        /// </summary>
        public Dictionary<string, long>? Categories { get; set; }

        /// <summary>
        /// 按标签聚合
        /// </summary>
        public Dictionary<string, long>? Tags { get; set; }

        /// <summary>
        /// 按状态聚合
        /// </summary>
        public Dictionary<string, long>? Statuses { get; set; }

        /// <summary>
        /// 按作者聚合
        /// </summary>
        public Dictionary<string, long>? Authors { get; set; }

        /// <summary>
        /// 按时间聚合（按月）
        /// </summary>
        public Dictionary<string, long>? DateHistogram { get; set; }
    }
}
